<?php
namespace Admin\Controller;

class GlobalController extends \Admin\Controller\AdminController
{
	/**
	 * 全局描述信息列表
	 * @author 尹雷
	 */
	public function apiNoteList()
	{
		$con = I('condition');
		$where['status'] = array('egt', 0);
		$where['super'] = 1;
		
		//查询条件
		if($con) {
			if(preg_match('/^[1-9]\d*$/', $con)) {
				$where['id'] = $con;
			} else {
				$where['name'] = array('like', '%'.$con.'%');
			}
		}
		
		//获取备注列表信息
		$list = $this->lists('GlobalNote', $where);
		
		if($list) {
			int_to_string($list);
			foreach($list as &$val) {
				$val['type_text'] = C('GLOBAL_NOTE')[1][1][$val['type']];
			}
		}
		
		//die(json_encode($list));
		$this->assign('action', array('list'=>'apiNoteList','super'=>1));
		$this->assign('_title', '资讯信息列表');
		$this->assign('_list', $list);
		$this->display('note_list');
	}
	
	/**
	 * TODO:禁用的业务逻辑待定
	 * 更新状态
	 * @author 尹雷
	 */
	public function changeNoteStatus($method=null)
	{
		!$method && $this->error('参数错误');
		$this->changeRecodeStatus($method, 'GlobalNote');
	}
	
	/**
	 * 添加资讯
	 * @author 尹雷
	 */
	public function addNote()
	{
		$super = I('super', '', '/^\d$/');
		!$super && $this->error('参数错误1');
		
		if(IS_GET) {
			$this->assign('type', C('GLOBAL_NOTE')[$super][1]);
			$this->assign('action', C('GLOBAL_NOTE')[$super][0]);
			$this->assign('list', $list);
			$this->assign('_title', '新增资讯');
			$this->display('add_edit_note');
		}
		//添加全局备注
		$res = D('GlobalNote')->addNote();
		if($res['error'] != 0) $this->error($res['msg']);
		$this->success('添加成功', U('Global/'.C('GLOBAL_NOTE')[$super][0]));
	}
	
	/**
	 * 编辑资讯
	 * @author 尹雷
	 */
	public function editNote()
	{
		$id = I('id', '', '/^\d+$/');
		!$id && $this->error('参数错误');
		$super = I('super', '', '/^\d$/');
		!$super && $this->error('参数错误');

		if(IS_GET) {
			//查询全局备注信息
			$note = M('GlobalNote')->where(array('id' => $id))->find();
			!$note && $this->error('查询失败');
			$this->assign('type', C('GLOBAL_NOTE')[$super][1]);
			$this->assign('note', $note);
			$this->assign('action', C('GLOBAL_NOTE')[$super][0]);
			$this->assign('_title', '编辑资讯');
			$this->display('add_edit_note');
		}
		if(I("type") == '202'){
			$content = I("content");
			$name = I("name");
			$isloginpush = I("is_login_push");
			$content = array("id"=>$id,"name"=>$name,"content"=>$content,"is_login_push"=>$isloginpush);
			$result = sendjpush(json_encode($content));

			if($result->data->msg_id>0){

			}
		}
		//添加全局备注
		$res = D('GlobalNote')->editNote();
		if($res['error'] != 0) $this->error($res['msg']);
		$this->success('添加成功', U('Global/'.C('GLOBAL_NOTE')[$super][0]));
	}
	
	/**
	 * 资讯详情
	 * @author 尹雷
	 */
	public function noteDetail()
	{
		$id = I('id', '', '/^\d+$/');
		!$id && $this->error('参数错误');
		$super = I('super', '', '/^\d$/');
		!$super && $this->error('参数错误');
		//查询全局备注信息
		$note = $this->returnOne('GlobalNote', array('id' => $id));
		!$note && $this->error('查询失败');
		$note['type_text'] = C('GLOBAL_NOTE')[$super][1][$note['type']];
		$note = array($note);
		int_to_string($note);
		$this->assign('note', $note[0]);
		$this->assign('list', C('GLOBAL_NOTE')[$super][0]);
		$this->assign('_title', '资讯详情');
		$this->assign('action', C('GLOBAL_NOTE')[$super][0]);
		$this->display('note_detail');
	}
	
	/**
	 * 全局配置信息列表
	 * @author 尹雷
	 */
	public function configList()
	{
		$con = I('condition');
		$where['status'] = array('egt', 0);
	
		//查询条件
		if($con) {
			if(preg_match('/^[1-9]\d*$/', $con)) {
				$where['id'] = $con;
			} else {
				$where['name'] = array('like', '%'.$con.'%');
			}
		}
	
		//获取备注列表信息
		$list = $this->lists('GlobalConfig', $where);
	
		if($list) {
			int_to_string($list);
		}
	
		//die(json_encode($list));
		$this->assign('_title', '全局配置列表');
		$this->assign('_list', $list);
		$this->display('config_list');
	}
	
	/**
	 * TODO:禁用的业务逻辑待定
	 * 更新状态
	 * @author 尹雷
	 */
	public function changeConfigStatus($method=null)
	{
		!$method && $this->error('参数错误');
		$this->changeRecodeStatus($method, 'GlobalConfig');
	}
	
	/**
	 * 添加全局配置
	 * @author 尹雷
	 */
	public function addConfig()
	{
		if(IS_GET) {
			$this->assign('list', $list);
			$this->assign('_title', '新增全局配置');
			$this->display('add_edit_config');
		}
	
		//添加全局配置
		$res = D('GlobalConfig')->addConfig();
		$this->cacheConfig();
		if($res['error'] != 0) $this->error($res['msg']);
		$this->success('添加成功', U('Global/configList'));
	}
	
	/**
	 * 编辑全局配置
	 * @author 尹雷
	 */
	public function editConfig()
	{
		$id = I('id', '', '/^[1-9]\d*$/');
		if(!$id) $this->error('参数错误');
		
		if(IS_GET) {
			$config = $this->returnOne('GlobalConfig', array('id'=>$id));
			if(!$config) $this->error('查询失败');
			$this->assign('list', $list);
			$this->assign('config', $config);
			$this->assign('_title', '编辑全局配置');
			$this->display('add_edit_config');
		}
	
		//编辑全局配置
		$res = D('GlobalConfig')->editConfig();
		$this->cacheConfig();
		if($res['error'] != 0) $this->error($res['msg']);
		$this->success('编辑成功', U('Global/configList'));
	}
	
	/**
	 * 全局配置详情
	 * @author 尹雷
	 */
	public function configDetail()
	{
		$id = I('id', '', '/^\d+$/');
		!$id && $this->error('参数错误');
		//查询全局配置信息
		$config = $this->returnOne('GlobalConfig', array('id' => $id));
		!$config && $this->error('查询失败');
		$config = array($config);
		int_to_string($config);
		$this->assign('config', $config[0]);
		$this->assign('_title', '全局配置详情');
		$this->display('config_detail');
	}
	
	/**
	 * 缓存全局配置
	 * @author 尹雷
	 */
	public function cacheConfig()
	{
		$config = $this->returnList('GlobalConfig','', '', 'id,name,value');
		if(!$config) $this->ajaxReturn(array('error' => 1, 'msg' => '更新失败'));
		
		//将name作为每个配置项的键名
		$tmp = array();
		foreach ($config as $value) {
			$tmp[$value['name']] = $value;
		}

		$config = $tmp;
		S(C('CONFIG_CACHE_NAME'), $config, array('expire' => 365*24*60*60));
		$this->ajaxReturn(array('error' => 0, 'msg' => '更新成功'));
	}
	
	/**
	 * 产生安全字符串
	 */
	public function generateSecretKey()
	{
		$str = \Common\Util\Util::randStr(32, 2);
		$this->ajaxReturn(array('error' => 1, 'msg' => hash('md5', $str)));
	}
	
	/**
	 * 资讯列表
	 * @author 尹雷
	 */
	public function newsList()
	{
		$con = I('condition');
		$where['status'] = array('egt', 0);
		$where['super'] = 2;
	
		//查询条件
		if($con) {
			if(preg_match('/^[1-9]\d*$/', $con)) {
				$where['id'] = $con;
			} else {
				$where['name'] = array('like', '%'.$con.'%');
			}
		}
	
		//获取资讯列表信息
		$list = $this->lists('GlobalNote', $where);
	
		if($list) {
			int_to_string($list);
			foreach($list as &$val) {
				$val['type_text'] = C('GLOBAL_NOTE')[2][1][$val['type']];
			}
		}
	
		//die(json_encode($list));
		$this->assign('action', array('list'=>'newsList','super'=>2));
		$this->assign('_title', '资讯信息列表');
		$this->assign('_list', $list);
		$this->display('note_list');
	}
}